import { LayerType, Model } from "../Model";

export class ModelAnalyzer{
    
    public static getDefaultInputName(model: Model): string {
        const modelHasOnlyOneInput: boolean = model.inputs.length === 1;
        if (modelHasOnlyOneInput) {
            return model.inputs[0].name;
        }

        const memories: Set<string> = new Set<string>();
        for (const m of model.memories) {
            memories.add(m.input);
        }

        // find the first unconnected input as a default model input
        const previousLayerNames: Set<string> = new Set<string>();
        for (const l of model.layers) {
            previousLayerNames.add(l.name);

            const layerDoesNotNeedInput: boolean = (l.type === LayerType.Load);

            if (layerDoesNotNeedInput) {
                continue;
            }

            for (const inputName of l.inputs) {
                const inputIsUnconnected: boolean = !previousLayerNames.has(inputName);
                const inputIsNotPartOfMemory: boolean = !memories.has(inputName);

                if (inputIsUnconnected && inputIsNotPartOfMemory) {
                    return inputName;
                }
            }
        }

        return "";
    }

    public static getDefaultOutputName(model: Model): string {
        if (model.outputs.length === 1) {
            return model.outputs[0];
        }

        if (model.layers.length > 0) {
            const lastLayer = model.layers[model.layers.length - 1];
            return lastLayer.name;
        }

        return "";
    }

}